package defineProduct.controller;

import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.gson.Gson;

import defineProduct.model.ItemBean;
import model.Brand;
import model.CategoryBean;
import model.DatabaseException;
import model.Material;
import model.Present;
import model.Producer;
import model.User;
import model.Utilities;

/**
 * Servlet implementation class DefineBasicProductFeatures
 */
@WebServlet("/DefineBasicProductFeatures1")
public class DefineBasicProductFeatures extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null || !user.getAccountType().equals("S")){
			response.sendRedirect("Login.html");
			return;
		}
		
		
		try {
			Iterator<Brand> brands = Utilities.getBrands();
			Iterator<Producer> producers = Utilities.getProducers();
			Iterator<Present> presents = Utilities.getPresents(user.getId());
			Iterator<Material> materials = Utilities.getMaterials();
			Iterator<CategoryBean> categories = Utilities.getCategories();
			Map<Long, String>  colors = Utilities.getColors();
			Map<Long, String> sizes = Utilities.getSizes();
			session.setMaxInactiveInterval(-1);
			session.setAttribute("colors", colors);
			session.setAttribute("sizes", sizes);
			session.setAttribute("define", "product");
			request.setAttribute("brands", brands);
			request.setAttribute("producers", producers);
			request.setAttribute("presents", presents);
			request.setAttribute("materials", materials);
			request.setAttribute("categories", categories);
			RequestDispatcher dispatcher = request.getRequestDispatcher("DefineProduct/DefineBasicProductFeatures1.jsp");
			dispatcher.forward(request, response);
		} catch (DatabaseException e) {
				response.sendRedirect("TechnicalDifficulties.html");
		}
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if(user==null || !user.getAccountType().equals("S")){
			response.sendRedirect("Login.html");
			return;
		}
		request.setCharacterEncoding("UTF-8");
		int supplierId = 4;
		Map<String,String[]> parameterMap = request.getParameterMap();
		String description = request.getParameter("description");
		String promotionalPrice = request.getParameter("promotional_price");
		Double promotionalPriceDouble = (!promotionalPrice.isEmpty())? Double.parseDouble(promotionalPrice) : 0;
		Double price = Double.parseDouble(request.getParameter("price"));
		
		String wholesalePrice = request.getParameter("wholesale_price");
		Double wholesalePriceDouble = (!wholesalePrice.isEmpty())?Double.parseDouble(wholesalePrice) : 0;
		int producerId = Integer.parseInt(request.getParameter("producerId"));
		int brandId = Integer.parseInt(request.getParameter("brandId"));
		String[] materialIds = parameterMap.get("materialIds[]");
		String[] materialPercents = parameterMap.get("materialPercents[]");
		String[] presentIds = parameterMap.get("presentIds[]");
		String[] features = parameterMap.get("features[]");
		String[] categoryIds = parameterMap.get("categoryIds[]");
		ItemBean item = new ItemBean(description, promotionalPriceDouble, price, 
				wholesalePriceDouble, brandId, producerId, supplierId);
		for(int i=0;i<materialIds.length; i++){
			item.addMaterialPercent(Long.parseLong(materialIds[i]), Integer.parseInt(materialPercents[i]));
		}
		for(String presentId :presentIds){
			if(presentId.equals("0")){
				continue;
			}
			item.addPresent(Long.parseLong(presentId));
		}
		for(String feature: features){
			if(feature.trim().isEmpty()){
				continue;
			}
			item.addFeature(feature);
		}
		
		for(String categoryId: categoryIds){
			item.addCategory(Long.parseLong(categoryId));
		}
		
		session.setAttribute("item", item);
//		Gson gson = new Gson();
//		System.out.println(gson.toJson(item));
		response.sendRedirect("DefineProduct/ChooseColorAndSize2.jsp");
		
		
//		System.out.println("Description: "+description);
//		System.out.println("Price: "+price);
//		System.out.println("Promotional price: "+promotionalPrice);
//		System.out.println("Wholesale price: "+ wholesalePrice);
//		System.out.println("ProducerId: "+producerId);
//		System.out.println("BrandId: " + brandId);
//		for(int i=0;i<materialIds.length; i++){
//			System.out.println("Material_id: "+materialIds[i]);
//			System.out.println("Material percent: "+materialPercents[i]);
//		}
//		System.out.println("Present ids:");
//		for(String present: presentIds){
//			System.out.println(present);
//		}
//		
//		System.out.println("Features: ");
//		for(String feature: features){
//			System.out.println(feature);
//		}
		
//		System.out.println("Categories: ");
//		for(String category: categoryIds){
//			System.out.println(category);
//		}
	}

}
